//
//  MYGBaseController.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/2/23.
//

import UIKit

class MYGBaseController: UIViewController {
    var hideNavigationController: Bool {
        false
    }
    var navigationTitleColor: UIColor {
        .rgba(51, 51, 51, 1)
    }
    var navigationBackgroundColor: UIColor {
        .clear
    }
    
    var navigationShadowColor: UIColor {
        .clear
    }
    
    lazy var popBackBtn: UIButton = {
        let control = UIButton(type: .custom)
        control.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
        control.setImage(UIImage(named: "comonback"), for: .normal)
        control.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        return control
    }()
    private var lastAppearnce: UINavigationBarAppearance?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        if self.navigationController?.viewControllers.count ?? 0 > 1 {
            let barBtnItem:UIBarButtonItem = UIBarButtonItem(customView: popBackBtn)
            self.navigationItem.leftBarButtonItem = barBtnItem
        }
        if let _ = parent {
            return
        }
        self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: navigationTitleColor, .font: UIFont.regular(18)]

    }
    func navigationBarHidden(){
        self.navigationController?.navigationBar.isHidden = true
    }
    func navigationBarShow(){
        self.navigationController?.navigationBar.isHidden = false
    }
    @objc func backAction() {
        if (self.presentingViewController != nil) == true{
            self.dismiss(animated: true)
        }
        self.navigationController?.popViewController(animated: true)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        lastAppearnce = self.navigationController?.navigationBar.standardAppearance
        navigationController?.setNavigationBarHidden(hideNavigationController, animated: true)
        
//        updateNavStyle()//导航栏会闪一下灰色
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        if let lastAppearnce = lastAppearnce {
            self.navigationController?.navigationBar.standardAppearance = lastAppearnce
            self.navigationController?.navigationBar.scrollEdgeAppearance = lastAppearnce
        }
    }
    
    func updateNavStyle() {
        let appearnce = UINavigationBarAppearance()
        appearnce.backgroundEffect = nil
        appearnce.backgroundColor = navigationBackgroundColor
        appearnce.shadowColor = navigationShadowColor
        self.navigationController?.navigationBar.standardAppearance = appearnce
        self.navigationController?.navigationBar.scrollEdgeAppearance = appearnce
    }
    
    // 是否支撑主动转屏
    override var shouldAutorotate: Bool {
        return false
    }
    // 支撑哪些屏幕方向
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    // 默许的屏幕方向（当时ViewController有必要是经过模态出来的UIViewController（模态带导航的无效）办法展现出来的，才会调用这个办法）
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .portrait
    }
}
